import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getDatabaseConfig } from './config/database.config';
import { UsersModule } from '@modules/users/users.module'
import { UsersController } from '@modules/users/users.controller';
import { AuthModule } from '@modules/auth/auth.module'
import { AuthController } from '@modules/auth/auth.controller';
import session from 'express-session';

@Module({
  imports: [
    ConfigModule.forRoot({ isGlobal: true }),  // Add this line
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: getDatabaseConfig,
      inject: [ConfigService],
    }),
    UsersModule,
    AuthModule
  ],
  controllers: [AppController, UsersController, AuthController],
  providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(
        session({
          secret: 'your-secret-key',
          resave: false,
          saveUninitialized: false,
          cookie: { maxAge: 60000 * 30 }, // 30分钟
        }),
      )
      .forRoutes('*');
  }
}
